package com.maochang.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maochang.pojo.PsRole;
import com.maochang.pojo.PsUser;
import com.maochang.pojo.util.Result;
import com.maochang.pojo.util.Result1;
import com.maochang.service.PsRoleService;
import com.maochang.service.PsUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/xiTongGuAnLi")
public class xiTongGuanLi {

    @Resource
    private PsUserService psUserServiceImpl;

    @Resource
    private PsRoleService PsRoleServiceImpl;

    //用户管理
    @GetMapping("/YongHuGuAnLi")
    public ModelAndView YongHuGuLi() throws IOException {
        return new ModelAndView("xiTongGuanLi/YongHuGuLi");
    }

    //角色管理
    @GetMapping("/JueSeGuanLi")
    public ModelAndView JueSeGuanLi() throws IOException {
        return new ModelAndView("xiTongGuanLi/JueSeGuanLi");
    }

    //角色权限管理
    @GetMapping("/JiaoSeQuanXianPeiZhi")
    public ModelAndView JiaoSeQuanXianPeiZhi() throws IOException {
        return new ModelAndView("xiTongGuanLi/JiaoSeQuanXianPeiZhi");
    }
    //系统配置
        @GetMapping("/xiTongPaiZhi")
    public ModelAndView xiTongPeiZhi() throws IOException {
        return new ModelAndView("xiTongGuanLi/xiTongPaiZhi");
    }
    //查询 用户
    @GetMapping("/ChaYongHu")
    public Result1  ChaYong(String userName,String userCode, @RequestParam("page") int current, @RequestParam("limit") int size){
        Page<PsUser> page =psUserServiceImpl.getUserAll(current,size,userName,userCode);
        return  Result1.success1(page);
    };

    //查询所有角色
    @GetMapping("/ChaJueSe")
    public Result1  ChaJueSe(@RequestParam("page") int current, @RequestParam("limit") int size){
        Result1 aa=new Result1();
        Page<PsRole> page =PsRoleServiceImpl.PsRoleList(current,size);
        aa.setData(page.getRecords());
        aa.setCount(Math.toIntExact(page.getTotal()));
        return  aa;
    };
    /**
     * 改用户状态
     * @return
     */
    @PostMapping("/XiuGaiYongHuZhuAngTai")
    public  Result1 XiuGaiYongHuZhuAngTai(Integer id,String zhuAngTai){
        Result1 aa=new Result1();
        Boolean c=psUserServiceImpl.UpDateZhuAngTai(id,zhuAngTai);
        if(c=true){
            aa.setMsg("状态修改成功");
            return aa;
        }
        aa.setMsg("修改状态失败");
        return aa;
    }
    /**
     * 修改用户信息
     */
    @PostMapping("/XiuGaiYongHuXinXi")
    public  Result1 XiuGaiYongHuXinXi(PsUser psUser){
        Result1 aa=new Result1();
        Boolean c=psUserServiceImpl.UpdatePsUserMessage(psUser);
        if(c.equals(true)){
            aa.setMsg("信息修改成功");
            return aa;
        }
        aa.setMsg("信息修改失败");
        return aa;
    }
    /**
     * 查询角色 下拉框赋值
     * @return
     */
    @PostMapping("/ChaXunJueSe")
    public List<PsRole> ChaXunJueSe(){
        List<PsRole> c=PsRoleServiceImpl.chaXunJueSe();
        return c;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/ShanChuYongHu")
    public Result ShanChuYongHu(int id){
        Boolean aBoolean = psUserServiceImpl.ShanChuYongHu(id);
        Result aa=new Result();
        if(aBoolean.equals(true)){
            aa.setMsg("删除成功");
        }else {
            aa.setMsg("删除失败");
        }
        return aa;
    }

    /**
     * 新增用户
     * @param psUser
     * @return
     */
    @PostMapping("/XinZengYongHu")
    public Result ShanChuYongHu(@RequestBody PsUser psUser){
        Boolean aBoolean = psUserServiceImpl.addUser(psUser);
        Result aa=new Result();
        if(aBoolean.equals(true)){
            aa.setMsg("新增成功");
        }else {
            aa.setMsg("新增失败");
        }
        return aa;
    }

}
